// Modified from http://www.exampledepot.com/egs/java.io/TraverseTree.html

package filesync;

import java.io.File;

public class FileSystemVisitor {
    private FileSystemVisitorListener listener;

    public FileSystemVisitor(FileSystemVisitorListener listener) {
        this.listener = listener;
    }

    public void visitAllDirsAndFiles(File dir) {
        if (dir.isDirectory()) {
            listener.onDirectory(dir);
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                visitAllDirsAndFiles(new File(dir, children[i]));
            }
        } else
            listener.onFile(dir);
    }

    public void visitAllDirs(File dir) {
        if (dir.isDirectory()) {
            listener.onDirectory(dir);

            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                visitAllDirs(new File(dir, children[i]));
            }
        }
    }

    public void visitAllFiles(File dir) {
        if (dir.isDirectory()) {
            String[] children = dir.list();
            for (int i=0; i<children.length; i++) {
                visitAllFiles(new File(dir, children[i]));
            }
        } else {
            listener.onFile(dir);
        }
    }
}
